package com.alogic.swagger.core;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * Paths
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerPath extends Segment {
    private String $name;
    private String $summary;
    private String $desc;
    private String $ref;

    public SwaggerPath(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);

        $name = PropertiesConstants.getRaw(p,"name","");
        $summary = PropertiesConstants.getRaw(p,"summary","");
        $desc = PropertiesConstants.getRaw(p,"description","");
        $ref = PropertiesConstants.getRaw(p,"ref","");
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        XsObject newCurrent = current.getObjectChild("paths",true);
        String name = PropertiesConstants.transform(ctx,$name,"");
        if (StringUtils.isNotEmpty(name)) {
            XsObject newPath = newCurrent.getObjectChild(name,true);

            newPath.addProperty("summary",PropertiesConstants.transform(ctx,$summary,""));
            newPath.addProperty("description",PropertiesConstants.transform(ctx,$desc,""));
            newPath.addProperty("$ref",PropertiesConstants.transform(ctx,$ref,""));

            super.onExecute(root, newPath, ctx, watcher);
        }
    }
}